package com.literature.search.core.utils;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @ClassName EncodeUtil
 * @Description TODO
 * @Author abin
 * @Date 2023/4/10 20:43
 * @Version 1.0
 */
public class EncodeUtil {
    /*
    SHA(Secure Hash Algorithm，安全散列算法），数字签名等密码学应用中重要的工具，
    被广泛地应用于电子商务等信息安全领域。虽然，SHA与MD5通过碰撞法都被破解了，
    但是SHA仍然是公认的安全加密算法，较之MD5更为安全*/
    public static final String KEY_SHA = "SHA";

    public static String getEncodePassword(String password) throws IllegalStateException{
        BigInteger sha;
        byte[] inputData = password.getBytes();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(KEY_SHA);
            messageDigest.update(inputData);
            sha = new BigInteger(messageDigest.digest());
        } catch (Exception e) {
            throw new IllegalStateException();
        }
        return sha.toString(32);
    }
}
